package com.learning.jquizenglish.screen.profile;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;

import com.learning.jquizenglish.activity.DoQuizActivity;
import com.learning.jquizenglish.activity.MainActivity;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.HistoryItemModel;
import com.learning.jquizenglish.model.ProfileModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.model.UserRowModel;
import com.learning.jquizenglish.screen.doquiz.DoQuizSession;
import com.learning.jquizenglish.utility.DialogUtil;
import com.learning.jquizenglish.utility.DialogUtil.OnDialogListener;

public class UserActionExecutor {

	private static UserActionExecutor INSTANCE = null;
	
	public static UserActionExecutor getInstance(){
		if (INSTANCE == null) {
			INSTANCE = new UserActionExecutor();
		}
		return INSTANCE;
	}
	
	public void executeAction(View anchor,ProfileScreen frag,UserRowModel model){
		switch (model.action) {
		case EDIT_AVATAR:
			editAvatar(anchor, frag, model);
			break;
		case EDIT_NAME:
			editName(anchor, frag, model);
			break;
		case EDIT_LIVING:
			editLivingIn(anchor, frag, model);
			break;
		case EDIT_ABOUT:
			editAbout(anchor, frag, model);
			break;
		case START_USER_ACTIVITY:
			startUserActivity(anchor, frag, model);
			break;
		default:
			break;
		}
	}
	
	private void editAvatar(View anchor,ProfileScreen frag,UserRowModel model){
		frag.showPopupGetImage(anchor,model);
	}
	
	private void editName(View anchor,final ProfileScreen frag,UserRowModel model){
		DialogUtil.showInputDialog(frag.getActivity(), "Your Name",model.strSecond, new OnDialogListener() {
			
			@Override
			public void onPositive(String result) {
				if (result!=null && !result.equals("")) {
					ProfileModel model = ProfilePreference.getInstance().getProfile();
					if (!model.name.equals(result)) {
						ProfileModel updateModel = model;
						updateModel.name = result;
						frag.updateProfile(updateModel);
					}
				}
			}
			
			@Override
			public void onCancel() {
				
			}
		});
	}
	
	private void editLivingIn(View anchor,final ProfileScreen frag,UserRowModel model){
		DialogUtil.showInputDialog(frag.getActivity(), "Your Living", model.strSecond,new OnDialogListener() {
			
			@Override
			public void onPositive(String result) {
				if (result!=null && !result.equals("")) {
					ProfileModel model = ProfilePreference.getInstance().getProfile();
					if (!model.living_in.equals(result)) {
						ProfileModel updateModel = model;
						updateModel.living_in = result;
						frag.updateProfile(updateModel);
					}
				}
			}
			
			@Override
			public void onCancel() {
				
			}
		});
	}
	
	private void editAbout(View anchor,final ProfileScreen frag,UserRowModel model){
		DialogUtil.showInputDialog(frag.getActivity(), "About You",model.strSecond, new OnDialogListener() {
			
			@Override
			public void onPositive(String result) {
				if (result!=null && !result.equals("")) {
					ProfileModel model = ProfilePreference.getInstance().getProfile();
					if (!model.about_you.equals(result)) {
						ProfileModel updateModel = model;
						updateModel.about_you = result;
						frag.updateProfile(updateModel);
					}
				}
			}
			
			@Override
			public void onCancel() {
				
			}
		});
	}
	
	private void startUserActivity(View anchor,JQuizEnglishFragment frag,UserRowModel model){
		if (model.ob instanceof HistoryItemModel) {
			ArrayList<QuestionModel> listQuestions = ((HistoryItemModel)model.ob).turn_choise;
			if (listQuestions!=null) {
				DoQuizSession.getInstance().setListQuestions(listQuestions);
				Intent intent = new Intent(frag.getActivity(), DoQuizActivity.class);
				intent.putExtra(DoQuizActivity.DO_QUIZ_MODE_KEY, DoQuizActivity.RESULT_MODE);
				frag.getActivity().startActivity(intent);
			}
		}
	}
	
}
